﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using App.Interface;
using App.Model;
using App.MVC.App_Start;
using Newtonsoft.Json;

namespace App.MVC.Controllers
{
    public class ReviewController : BaseController
    {
        //审核工作计划
        // GET: Review
        public IMoBan _IMoBan { set; get; }
        public IMyCaoZuo _iMyCaoZuo { set; get; }
        public IPingShen _IPingShen { set; get; }
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 审核工作计划
        /// </summary>
        /// <returns></returns>
        public ActionResult ReviewGongZuoJiHua()
        {
            return View();
        }
        public JsonResult GetGZJHs()
        {
            var str = _IPingShen.GetBeiKaoHeBiaos(SiteInfo.GetUserName());
            Newtonsoft.Json.Converters.IsoDateTimeConverter convert = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            convert.DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff";
            List<被考核表> tt = JsonConvert.DeserializeObject<List<被考核表>>(str, convert);
            return Json(tt, JsonRequestBehavior.AllowGet);
        }
        //[HttpGet]
        //public JsonResult GetKaoHeBiaos(string datetimes, int mbid)
        //{
        //    var str = _IPingShen.GetKaoHeFanWeiKHB(datetimes, mbid, SiteInfo.GetUserName());
        //    List<MyKaoHeBiao> tt = new List<MyKaoHeBiao>();
        //    if (!string.IsNullOrWhiteSpace(str))
        //    {
        //        Newtonsoft.Json.Converters.IsoDateTimeConverter convert = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
        //        convert.DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff";
        //        tt = JsonConvert.DeserializeObject<List<MyKaoHeBiao>>(str, convert);
        //    }
        //    return Json(tt, JsonRequestBehavior.AllowGet);
        //}
        /// <summary>
        /// 获取所有的表
        /// </summary>
        /// <param name="mbid"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetAllIngKaoHeBiaos(int mbid)
        {
            var str = _IPingShen.GetAllIngKaoHeBiaos(mbid, SiteInfo.GetUserName());
            List<MyKaoHeBiao> tt = new List<MyKaoHeBiao>();
            if (!string.IsNullOrWhiteSpace(str))
            {
                Newtonsoft.Json.Converters.IsoDateTimeConverter convert = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
                convert.DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff";
                tt = JsonConvert.DeserializeObject<List<MyKaoHeBiao>>(str, convert);
            }
            return Json(tt, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取所有正在考核中的表
        /// </summary>
        /// <param name="mbid"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetAllIngKaoHeBiaosByKeShenHe(string datetimes, int mbid)
        {
            var str = _IPingShen.GetAllIngKaoHeBiaosByKeShenHe(datetimes, mbid, SiteInfo.GetUserName());
            List<MyKaoHeBiao> tt = new List<MyKaoHeBiao>();
            if (!string.IsNullOrWhiteSpace(str))
            {
                Newtonsoft.Json.Converters.IsoDateTimeConverter convert = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
                convert.DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff";
                tt = JsonConvert.DeserializeObject<List<MyKaoHeBiao>>(str, convert);
            }
            return Json(tt, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetKaoHeBiaoInfoGZJH(int mkhbid)
        {
            string str = _IPingShen.GetKaoHeBiaoInfoGZJH(mkhbid, SiteInfo.GetUserName());
            MyKaoHeBiao tt = JsonConvert.DeserializeObject<MyKaoHeBiao>(str);
            return Json(tt, JsonRequestBehavior.AllowGet);
        }

        public void GongZuoJiHuaOK(int mkhbid)
        {
            _iMyCaoZuo.UpdateGongZuoJiHuaOK(SiteInfo.GetUserName(), mkhbid,"");
        }
        public void GongZuoJiHuaReject(int mkhbid, string yijian)
        {
            // _iMyCaoZuo.UpdateGongZuoJiHuaReject(mkhbid);
            //_IPingShen.UpdateKaoHeBiao_TuiHui(mkhbid, SiteInfo.GetUserName(), yijian);
            _iMyCaoZuo.UpdateGongZuoJiHuaTuiHui(SiteInfo.GetUserName(), mkhbid, yijian);
        }

        public ActionResult KaoHe()
        {
            return View();
        }
        public JsonResult GetKaoHeBiaoInfo(int mkhbid)
        {
            string str = _IPingShen.GetKaoHeBiaoInfo(mkhbid, SiteInfo.GetUserName());
            MyKaoHeBiao tt = JsonConvert.DeserializeObject<MyKaoHeBiao>(str);
            return Json(tt, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取考核范围人员
        /// </summary>
        /// <returns></returns>
        public JsonResult GetKaoHeRenYuans()
        {
            string str = _IPingShen.GetKaoHeRenYuanList(SiteInfo.GetUserName());
            List<BeiKaoHeJiaoZhiGong> tt = JsonConvert.DeserializeObject<List<BeiKaoHeJiaoZhiGong>>(str);
            return Json(tt, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取考核表ByUsername
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public JsonResult GetKaoHeBiaos(string username)
        {
            string str = _IPingShen.GetKaoHeBiaos(username,SiteInfo.GetUserName());
            List<MyKaoHeBiao> tt = JsonConvert.DeserializeObject<List<MyKaoHeBiao>>(str);
            return Json(tt, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 保存评审结果
        /// </summary>
        /// <param name="model"></param>
        [HttpPost]
        public JsonResult SavePingShenResult(int khbid,List<JieGuoJi> data)
        {
            dynamic msg = new { data = "", error = false };
            KaoHeJieGuo jg=new KaoHeJieGuo();
            jg.khbid = khbid;
            jg.username = SiteInfo.GetUserName();
            jg.data = data;
            string str = JsonConvert.SerializeObject(jg);
            _IPingShen.SavePingShenJieGuo(str);
            //
            //foreach (var item in model.MyKaoHeBiaoOnes)
            //{
            //    foreach (var item2 in item.MyKaoHeBiaoTwos.Where(s => s.MyKaoHeBiaoTwoPingFens != null))
            //    {
            //        foreach (var item3 in item2.MyKaoHeBiaoTwoPingFens)
            //        {
            //            if (item2.fz < item3.fz)
            //            {
            //                msg = new { data = item2.mbnr + ":评分的分值大于设定分值，请得新评分", error = true };
            //                break;
            //                //throw new Exception("评分分值高于设定分值，请重新评分");
            //            }
            //            item3.pfusername = SiteInfo.GetUserName();
            //        }
            //    }
            //}

            //if (msg.error == true)
            //{

            //}
            //else
            //{
            //    string str = JsonConvert.SerializeObject(model);
            //    _IPingShen.SavePingShenJieGuo(str);
            //}
            return Json(msg, JsonRequestBehavior.AllowGet);

        }
        /// <summary>
        /// 生成考核结果_考核表转换为得分表
        /// </summary>
        /// <returns></returns>
        public ActionResult ConvertKaoHeBiao()
        {
            return View();
        }

        public void CreateKaoHeDeFenBiao(int ksid)
        {
            _IPingShen.CreateKaoHeDeFenBiao(ksid);
        }
        /// <summary>
        /// 科室微调
        /// </summary>
        /// <returns></returns>
        public ActionResult FanWeiNeiWeiTiao()
        {
            return View();
        }
        /// <summary>
        /// 获取考核范围内可微调得分表
        /// </summary>
        /// <param name="zwid"></param>
        /// <returns></returns>
        public JsonResult GetXiaShuZhiWuYuanGong()
        {
            List<KaoHeHuiZongBiao> tt = new List<KaoHeHuiZongBiao>();
            string str = _IPingShen.GetXiaShuZhiWuYuanGong(SiteInfo.GetUserName());
            tt = JsonConvert.DeserializeObject<List<KaoHeHuiZongBiao>>(str);
            return Json(tt, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 确认下属得分
        /// </summary>
        /// <param name="zwid"></param>
        public void QueRenXiaShuDeFen()
        {
            _IPingShen.QueRenXiaShuDeFen(SiteInfo.GetUserName());
        }

        /// <summary>
        /// 副校级微调
        /// </summary>
        public ActionResult KaoHeFanWeiWeiTiao()
        {
            return View();
        }

        public JsonResult GetKaoHeWeiTiao(int mbid = 0)
        {
            List<KaoHeHuiZongBiao> tt = new List<KaoHeHuiZongBiao>();
            string str = _IPingShen.GetKaoHeWeiTiao(SiteInfo.GetUserName(), mbid);
            tt = JsonConvert.DeserializeObject<List<KaoHeHuiZongBiao>>(str);
            return Json(tt, JsonRequestBehavior.AllowGet);
        }

        public ActionResult AllSchoolGongZuoJiHua()
        {
            return View();
        }
        #region 打印全校工作计划
        public ActionResult PrintGongZuoJiHua(int id)
        {
            ViewBag.id = id;
            return View();
        }
        #endregion
        public void DownLoadAll(string datetimes, int mbid)
        {
            var str = _IPingShen.GetAllIngKaoHeBiaosByKeShenHe(datetimes, mbid, SiteInfo.GetUserName());
            List<MyKaoHeBiao> tt = new List<MyKaoHeBiao>();
            if (!string.IsNullOrWhiteSpace(str))
            {
                Newtonsoft.Json.Converters.IsoDateTimeConverter convert = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
                convert.DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff";
                tt = JsonConvert.DeserializeObject<List<MyKaoHeBiao>>(str, convert);
            }
            List<aaa> tempa = new List<aaa>();
            foreach (var item in tt)
            {
                aaa model = new aaa();
                model.姓名 = item.xingming;
                if (item.khbzt == 1 || item.khbzt == 2)
                {
                    model.状态 = "编辑中";
                }
                if (item.khbzt == 3)
                {
                    model.状态 = "已提交";
                }
                if (item.khbzt == 4)
                {
                    model.状态 = "通过审核";
                }
                model.科室 = item.ksmc;
                model.职务 = item.zwmc;
                model.工作计划 = item.mkhbmc;
                tempa.Add(model);
            }
            DataTable dt = new DataTable();
            var dttools = new DataTableTOList();
            dt = dttools.ToDataTable(tempa);
            ToExcel(dt);
        }

        public class aaa
        {
            public string 工作计划 { set; get; }
            public string 姓名 { set; get; }
            public string 科室 { set; get; }
            public string 职务 { set; get; }
            public string 状态 { set; get; }
        }
        /// <summary>
        /// 用户导出Excle
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="keyword"></param>
        /// <param name="report"></param>
        /// <param name="info"></param>
        /// <param name="prof"></param>
        /// <param name="content"></param>
        /// <param name="sex"></param>
        /// <param name="youyixiang"></param>
        /// <param name="youji"></param>
        public void ToExcel(DataTable dt)
        {

            string fileName = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + ".xls";//客户端保存的文件名
            //     string filePath = Server.MapPath("DownLoad/aaa.xls");//路径
            //搜索类 _搜索类 = new 搜索类();
            //_搜索类 = JsonHelp.JsonDeserialize<搜索类>(searchstr);
            var bytes = 用户导出Excel(dt);
            Response.ContentType = "application/octet-stream";
            //通知浏览器下载文件而不是打开
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }
        public byte[] 用户导出Excel(DataTable dt)
        {
            System.Diagnostics.Stopwatch timeWatch = System.Diagnostics.Stopwatch.StartNew();
            var bb = NPOIExcelHelper.DataTableToExcel(dt, "", "mytest1.xls");
            timeWatch.Stop();
            return bb;
        }
        /// <summary>
        /// 工作计划调整
        /// </summary>
        /// <returns></returns>
        public ActionResult GongZuoJiHuaTiaoZheng()
        {
            return View();
        }

        public ActionResult SeeAll()
        {
            return View();
        }

        public ActionResult SeeAllOld()
        {
            return View();
        }

        public ActionResult PrintOld(int mkhbid)
        {
            ViewBag.mkhbid = mkhbid;
            string str = _IPingShen.GetKaoHeBiaoInfoGZJH(mkhbid, SiteInfo.GetUserName());
            MyKaoHeBiao tt = JsonConvert.DeserializeObject<MyKaoHeBiao>(str);
            ViewBag.Name = tt.xingming;
            ViewBag.KeShi = tt.ksmc;
            ViewBag.ZhiWu = tt.zwmc;
            ViewBag.sssj = tt.sssjstr;
            ViewBag.str = JsonConvert.SerializeObject(tt);
            return View();
        }
        public ActionResult KaoHeHuiZongDaBiao()
        {
            return View();
        }

        public JsonResult GetKaoHeMoBan(int mbid)
        {
            HuiZongMoBan tt = new HuiZongMoBan();
            string str = _IPingShen.GetKaoHeHuiZongMoBan(mbid);
            if (!string.IsNullOrWhiteSpace(str))
            {
                tt = JsonConvert.DeserializeObject<HuiZongMoBan>(str);
            }
            return Json(tt, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetKaoHeDeFen(int mbid,string sssj)
        {
            List<HuiZongDeFen> tt = new List<HuiZongDeFen>();
            string str = _IPingShen.GetKaoHeHuiZongDeFen(mbid, sssj);
            if (!string.IsNullOrWhiteSpace(str))
            {
                tt = JsonConvert.DeserializeObject<List<HuiZongDeFen>>(str);
            }
            return Json(tt.OrderByDescending(s => s.zongfen), JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetPingFenRenYuanList(int mkhbid)
        {
            List<MyKaoHeBiaoPingFen> tt = new List<MyKaoHeBiaoPingFen>();
            string str = _IPingShen.GetPingFenRenYuanList(mkhbid);
            if (!string.IsNullOrWhiteSpace(str))
            {
                tt = JsonConvert.DeserializeObject<List<MyKaoHeBiaoPingFen>>(str);
            }
            return Json(tt, JsonRequestBehavior.AllowGet);
        }

        public void DelPingFenRenYuan(int mkhbid, string username)
        {
            _IPingShen.DelPingFenRenYuan(mkhbid, username);
        }
        /// <summary>
        /// 向职工的工作计划中添加内容
        /// </summary>
        /// <returns></returns>
        public ActionResult AddGongZuoJiHua()
        {
            return View();
        }
    }
}